package edu.asu.sefcom.extract.profile;

import java.util.*;
import java.io.*;

public final class FileListing {

	public static void main(String... aArgs) throws FileNotFoundException {
		File startingDirectory = new File(aArgs[0]);
		List<File> files = FileListing.getFileListing(startingDirectory);

		for (File file : files) {
			System.out.println(file);
		}
	}

	static public List<File> getFileListing(File aStartingDir)
			throws FileNotFoundException {
		validateDirectory(aStartingDir);
		List<File> result = getFileListingNoSort(aStartingDir);
		Collections.sort(result);
		return result;
	}

	static private List<File> getFileListingNoSort(File aStartingDir)
			throws FileNotFoundException {
		List<File> result = new ArrayList<File>();
		File[] filesAndDirs = aStartingDir.listFiles();
		List<File> filesDirs = Arrays.asList(filesAndDirs);
		for (File file : filesDirs) {
			result.add(file); // always add, even if directory
			if (!file.isFile()) {
				// must be a directory
				// recursive call!
				List<File> deeperList = getFileListingNoSort(file);
				result.addAll(deeperList);
			}
		}
		return result;
	}

	static private void validateDirectory(File aDirectory)
			throws FileNotFoundException {
		if (aDirectory == null) {
			throw new IllegalArgumentException("Directory should not be null.");
		}
		if (!aDirectory.exists()) {
			throw new FileNotFoundException("Directory does not exist: "
					+ aDirectory);
		}
		if (!aDirectory.isDirectory()) {
			throw new IllegalArgumentException("Is not a directory: "
					+ aDirectory);
		}
		if (!aDirectory.canRead()) {
			throw new IllegalArgumentException("Directory cannot be read: "
					+ aDirectory);
		}
	}

	static public List<File> getFileListingForUser(File aStartingDir,
			String userName) throws FileNotFoundException {
		validateDirectory(aStartingDir);
		List<File> result = new ArrayList<File>();
		File[] filesAndDirs = aStartingDir.listFiles();
		List<File> filesDirs = Arrays.asList(filesAndDirs);
		for (File file : filesDirs) {
			if (file.isDirectory() && file.getName().equalsIgnoreCase(userName)) {
				System.out.println(file.getName());
				File[] filesAndDirs1 = file.listFiles();
				List<File> filesDirs1 = Arrays.asList(filesAndDirs1);
				for (File file1 : filesDirs1) {
					result.add(file1);
				}
				System.out.println(result);
				break;
			} else if (file.isDirectory()) {

				result.addAll(getFileListingForUser(file, userName));
			}
		}
		return result;
	}

	static public Set<String> getDirListingForGroups(File aStartingDir)
			throws FileNotFoundException {
		validateDirectory(aStartingDir);
		Set<String> result = new HashSet<String>();
		File[] filesAndDirs = aStartingDir.listFiles();
		List<File> filesDirs = Arrays.asList(filesAndDirs);
		for (File file : filesDirs) {
			if (file.isDirectory()) {
				System.out.println(file.getName());
				File[] filesAndDirs1 = file.listFiles();
				List<File> filesDirs1 = Arrays.asList(filesAndDirs1);
				for (File file1 : filesDirs1) {
					if (file1.isDirectory()) {
						result.add(file1.getName());
					}
				}
			}
		}
		return result;
	}

}